URL = "http://downloads.sourceforge.net/project/ftgl/FTGL Source/2.1.3~rc5/ftgl-2.1.3-rc5.tar.gz"
DEFAULT_VERSION = "2.1.3-rc5"
VERSIONS = ["2.1.3-rc5"]
DEPENDS = ["freetype"]
#todo other version support

from source.builders import ConfigureMakeBuilder, VisualStudioBuilder
import os

#Note if latex is installed documentation will be build
#it might be that the script then also needs the latex utf8 package

# 64 bit windows not fully supported,
# 
# find and replace win32 into x64 in ftgl.sln
#
# still needs to be added to the script
#
# also freetype does not have a x64 solution

class FTGLConfigureMakeBuilder( ConfigureMakeBuilder ):
  """ subclass of ConfigureMakeBuilder with custom configure step """
  def configureparameters(self):
    return ["--with-ft-prefix="+self.dependencies["freetype"].targetpath()]


class FTGLWindowsBuilder( VisualStudioBuilder ):

  def installpath(self):
    return os.path.join(self.targetpath(), "binaries")

  def make(self):
    os.chdir( self.extractedpath )
    import build_freetype
    env = os.environ

    env['FREETYPE'] = self.dependencies["freetype"].targetpath()

    self.debug = False
    #vc9 unavailable so we upgrade the solution vc = {0: "vc8", 1: "vc9"}[self.machine.isVS2008()] 
    debug = {0: "Release", 1: "Debug"}[self.debug]
    
    if self.machine.isVS2008():
      self.execute( "devenv.com msvc\\vc8\\ftgl.sln /Upgrade" )
    
    replacements = []
    if self.machine.bits == 64:
      self.convertSolutionTo64("msvc\\vc8\\ftgl.sln")      
      replacements += self.getx64replacements()
      replacements.append(( "^				TargetMachine=\"1\"", "TargetMachine=\"17\""))
    replacements.append(( "^				AdditionalDependencies", "AdditionalDependencies=\"odbc32.lib odbccp32.lib opengl32.lib glu32.lib freetype2311.lib\""))
    replacements.append(( "				AdditionalLibraryDirectories", "AdditionalLibraryDirectories=\"$(FREETYPE)\\lib \""))     
    self.replaceLinesInFile( "msvc\\vc8\\ftgl_dll.vcproj", replacements )
    
    self.execute( "devenv.com msvc\\vc8\\ftgl.sln /Build "+debug+" /project \"ftgl_dll.vcproj\" ", env=env )
    
  def install(self ):
    pass

  def copy(self):
    os.chdir( self.extractedpath )
    self.copydir( "src\\FTGL\\", self.targetpath()+"/include//FTGL")
    self.copyfile( "msvc\\build\\ftgl.lib", self.targetpath()+"/lib")
    self.copyfile( "msvc\\build\\ftgl.dll", self.targetpath()+"/lib")

def decider( machine, dirs ):
  return { "Linux" : FTGLConfigureMakeBuilder,
           "Windows" : FTGLWindowsBuilder }[machine.platform]

BUILDER = decider


